package util;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class Sqlite {
    public static List<Map<String, Object>> getFinanceObject() {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            @Cleanup Connection connection = null;
            @Cleanup Statement statement = null;

            // 加载SQLite驱动程序
            Class.forName("org.sqlite.JDBC");

            // 创建数据库连接
            connection = DriverManager.getConnection("jdbc:sqlite:./sqlite_data/stock.sqlite");

            // 创建Statement对象
            statement = connection.createStatement();

            // 创建表
            String createTableSQL = "select * from finance";
            @Cleanup ResultSet rs = statement.executeQuery(createTableSQL);

            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                ResultSetMetaData rsm = rs.getMetaData();
                int count = rsm.getColumnCount();

                for (int i = 0; i < count; i++) {
                    // 获取字段名
                    String name = rsm.getColumnName(i + 1);
                    map.put(name, rs.getObject(name));
                }
                list.add(map);
            }

        } catch (ClassNotFoundException | SQLException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        return list;
    }
}
